<?php

//个人主页
require 'lib.php';
include('header.php');
//1.判断是否登录
if( !($user = isLogin()) ) {
    header('Location: index.php');exit(0);
}

$redis = redis_conn();
//2.查询 几个粉丝 几个关注
$fans_num = $redis->sCard('fans:userid:'.$user['userid']); //粉丝数
$following_num = $redis->sCard('following:userid:'.$user['userid']);// 关注数

// V1.0 微博博文设计为string类型 -- 读取方法
/*//3.1取出自己和关注的人发布的微博  -- spost:userid:3  -- sort 排序
$sort = array('sort' => 'desc', 'get' => 'post:postid:*:content');
$contents = $redis->sort('spost:userid:'.$user['userid'], $sort);
//3.2取出发布的用户id
$sort2 = array('sort' => 'desc', 'get' => 'post:postid:*:userid');
$user_ids = $redis->sort('spost:userid:'.$user['userid'], $sort2);
//3.2.1 根据用户id，查询用户名
$user_info = [];
foreach ( $user_ids as $k => $v ) {
    $user_info[$k]['userid'] = $v;
    $user_info[$k]['username'] = $redis->get('user:userid:'.$v.':username');
}
//3.3 查询发布时间
$sort3 = array('sort' => 'desc', 'get' => 'post:postid:*:time');
$post_time = $redis->sort('spost:userid:'.$user['userid'], $sort3);*/


//v1.1 拉取数据的操作 --- 存放到链表中保存1000条热数据
//① 查询所有的粉丝列表
$fans = $redis->sMembers('fans:userid:'.$user['userid']); //sadd('fans:userid:1', 2); //我的粉丝列表
$fans[] = $user['userid'];

//② 遍历粉丝数组，记录查询起点
$post_list = array();
foreach ($fans as $fan_userid) {
    //③ 记录查询起点
    $last_pull_start = $redis->get('startpullid:userid:'.$user['userid'].':fan_userid:'.$fan_userid);
    //④ 查询粉丝发布的微博，从pull:userid中 -- 存储为链表
    $pull_weibo_ids = $redis->zRangeByScore('pull:userid:'.$fan_userid, $last_pull_start+1, abs(1 << 31));

    //⑤ 如果pull有数据的话，更新 $last_pull_start
    if(count($pull_weibo_ids)) {
        sort($pull_weibo_ids,SORT_NUMERIC);
        $redis->set('startpullid:userid:'.$user['userid'].':fan_userid:'.$fan_userid, end($pull_weibo_ids));
        $post_list = array_merge($post_list, $pull_weibo_ids); //保存postid
    }
}
sort($post_list, SORT_NUMERIC);
//var_dump($post_list);

// 遍历数组,pull 拉取微博id到用户中
foreach ($post_list as $post_id) {
    $redis->lPush('spost:userid:'.$user['userid'], $post_id);
    //判断数量是否超出1000
    if( $redis->lLen('spost:userid:'.$user['userid']) > 1000) {
        $redis->lTrim('spost:userid:'.$user['userid'], 0, 999);
    }
}

// V1.0 发布为hash类型存储 -- 读取方法
//$sort = ['sort' => 'desc', 'hmget' => 'post:postid:*'];
$sort = ['sort' => 'desc'];
$postids = $redis->sort('spost:userid:'.$user['userid'], $sort); //获取发布的ids
//var_dump($postids);
?>

    <div id="postform">
        <form method="POST" action="post.php">
            <?php echo $_COOKIE['username']; ?>, 有啥感想?
            <br>
            <table>
                <tr>
                    <td><textarea cols="70" rows="3" name="status"></textarea></td>
                </tr>
                <tr>
                    <td align="right"><input type="submit" name="doit" value="Update"></td>
                </tr>
            </table>
        </form>
        <div id="homeinfobox">
            <?php echo $fans_num; ?>粉丝<br>
            <?php echo $following_num; ?>关注<br>
        </div>
    </div>
<?php /*foreach ($user_info as $kk => $vv) { */?><!--
    <div class="post">
        <a class="username" href="profile.php?u=<?php /*echo $vv['username']*/?>"><?php /*echo $vv['username']*/?></a> <?php /*echo $contents[$kk];*/?><br>
        <i><?php /*echo best_time( $post_time[$kk] );*/?> 分钟前 通过 web发布</i>

    </div>-->
<!-- 版本v1.0的设计 -->
    <?php
        foreach ($postids as $kk => $postid) {
            $post_info = $redis->hMGet('post:postid:'.$postid, ['userid', 'username', 'time', 'content']);
            //判断微博是否还存在，不存在的话，删除并break
            if (empty($post_info['content'])) {
                //删除 链表 spost:userid:$userid 的数据
                $rs = $redis->lRem('spost:userid:'.$user['userid'], $postid, 1);
                continue;
            }
    ?>
    <div class="post">
        <a class="username" href="profile.php?u=<?php echo $post_info['username']?>"><?php echo $post_info['username']?></a> <?php echo $post_info['content'];?><br>
        <i><?php echo best_time( $post_info['time'] );?> 通过 web发布</i>

    </div>

<?php } include 'footer.php';?>
